<template>
	<view class="page">
		<u-form :model="model" :rules="rules" ref="uForm" labelWidth="70" :errorType="errorType" labelPosition="left">

			<u-form-item label="昵称" prop="nickname" borderBottom labelPosition="left">
				<u-input placeholder="请输入昵称" v-model="model.nickname" type="text" clearable border="none">
				</u-input>
			</u-form-item>

			<u-form-item label="性别" prop="gender" borderBottom @click="actionSheetShow = true">
				<u-input type="text" v-model="model.gender" placeholder="请选择性别" disabled disabledColor="#ffffff"
					border="none"></u-input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>

			<u-form-item label="手机号码" borderBottom>
				<u-input placeholder="请输入手机号" v-model="model.mobile" type="number" border="none"></u-input>
				<u-button size="mini" slot="right" type="primary" open-type="getPhoneNumber"
					@getphonenumber="getphonenumber">绑定手机号</u-button>
			</u-form-item>

			<u-form-item label="生日" prop="birthday" borderBottom @click="birthdayCalenderShow = true">
				<u-input placeholder="请输入生日" v-model="model.birthday" :select-open="birthdayCalenderShow" border="none">
				</u-input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>

		</u-form>
		<view class="page" style="margin-top: 60rpx;">
			<u-button type="primary" @click="submit">提交</u-button>
		</view>

		<u-action-sheet :actions="actionSheetList" :show="actionSheetShow" @select="actionSheetCallback"
			description="如果选择保密会报错" @close="actionSheetShow = false">
		</u-action-sheet>
		<u-datetime-picker ref="datetimePicker" :show="birthdayCalenderShow" :minDate="Number(new Date('1950-01-01'))"
			:maxDate="Number(new Date())" @cancel="birthdayCalenderShow = false" @confirm="setBirthday" mode="date">
		</u-datetime-picker>

	</view>
</template>

<script>
	import {
		http_getUserInfo,
		http_setUserInfo,
		http_getUserPhoneFromMP
	} from '@/common/api.js';
	export default {
		data() {
			return {
				model: {
					avatar: '',
					nickname: '',
					gender: '',
					mobile: '',
					birthday: '',
				},
				rules: {
					mobile: [{
						validator: (rule, value, callback) => {
							if (value) {
								return uni.$u.test.mobile(value);
							}
							return true
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ['change', 'blur'],
					}],
					nickname: [{
						required: true,
						message: '请输入姓名',
						trigger: ['blur', 'change']
					}]
				},
				actionSheetList: [{
						name: '男'
					},
					{
						name: '女'
					},
					{
						name: '保密'
					}
				],
				actionSheetShow: false,
				birthdayCalenderShow: false,
				errorType: ['message'],
			};
		},
		onLoad() {
			uni.hideShareMenu()
			http_getUserInfo()
				.then(res => {
					this.model = Object.assign(this.model, res.data) // 合并code, 要不后面错误
				})
		},
		onReady() {
			this.$refs.datetimePicker.setFormatter(this.formatter)
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			getphonenumber(res) {
				if (res.detail.errMsg == 'getPhoneNumber:fail user deny') {

				} else if ("getPhoneNumber:ok" == res.detail.errMsg) {
					http_getUserPhoneFromMP({
							encryptedData: res.detail.encryptedData,
							iv: res.detail.iv,
						})
						.then(res => {
							console.log(res);
							this.model.mobile = res.data.phone
						})
				}
			},
			submit() {
				this.$refs.uForm.validate().then(res => {
					http_setUserInfo(this.model)
						.then(res => {
							console.log(res);
							if (1 == res.code) {
								this.$u.toast('保存成功')
								this.$u.vuex('vuex_user', res.data)
							} else {
								this.$u.toast(res.msg);
							}
						})
				}).catch(errors => {
					console.log('验证失败');
				})
			},
			// 点击actionSheet回调
			actionSheetCallback(e) {
				uni.hideKeyboard();
				this.model.gender = e.name
			},
			setBirthday(e) {
				console.log(e)
				this.model.birthday = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
				this.birthdayCalenderShow = false
			}
		}
	};
</script>

<style scoped lang="scss">
	.page {
		padding: 0 24rpx;
	}

	.divider {
		width: 100%;
		height: 16rpx;
		background-color: #F3F3F3;
	}
</style>
